// 封装request请求
const BASE_URL = 'http://exam.canplay.com.cn/student'

export default function fetch(type,url,data){
	return new Promise((resolve,reject) => {
		uni.request({
			url: `${this.globalData.baseUrl}${url}`,
			data: data,
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			method: type,
			success: function(res) {
				if(res.data.code !== 200) {
						uni.showToast({
							title: '请求出错',
							icon: 'none',
							duration: 2000
						})
					// return reject(res)
				}
				resolve(res.data)
			},
			fail: function(res) {
				wx.showToast({
					title: '请求出错',
					icon: 'none',
					duration: 2000
				})
				// reject(res)
			}
		})
	})
}

function get(url, data) {
	let commonData = {
		platform: 3
	}
	let getData = Object.assign({}, data, commonData)
	return this.fetch('GET', url, getData)
}

function post(url, data) {
	let commonData = {
		token: this.globalData.token,
		userId: this.globalData.userId,
		times: Date.now(),
		platform: 3
	}
	let postData = Object.assign({}, data, commonData)
	return this.fetch('POST', url, postData)
}

export {get, post}